package cn.dida.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;

import cn.dida.exception.BizzException;
import cn.dida.model.GeneralSet;
import cn.dida.model.HttpResponseBodyWrapper;
import cn.dida.model.Pageable;
import cn.dida.model.Staff;
import cn.dida.model.UserSession;
import cn.dida.service.GeneralSetService;
import cn.dida.util.DateUtils;
import cn.dida.util.ExcelUtil;
import net.sf.json.JSONObject;

/**
 * 系统管理》通用设置
 * @author kelan
 * @date 2017年4月11日
 */
@Controller
public class GeneralSetController extends BaseController {
    @Autowired
    private GeneralSetService generalsetService;

  
    /**
     * 通用设置列表
     * @author kelan
     * @date 2017年4月11日
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping("/generalsettings")
    public ModelAndView generalsetList(HttpServletRequest request) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);

        List<GeneralSet> GeneralSetList = generalsetService.query(userSession.getCompanyId());
        
        model.put("GeneralSetList", GeneralSetList);
        model.put("account",userSession.getAccount());
        model.put("admin",userSession.getAdmin());
        return new ModelAndView("generalset_list").addAllObjects(model);
    }

    /**
     * 保存方法
     * @author kelan
     * @date 2017年4月19日
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @PutMapping("/generalset/save")
    public HttpResponseBodyWrapper GeneralSetSave(HttpServletRequest request, @RequestBody Map<String, Object> generalsetList) throws Exception {
        //Map<String, Object> model = new HashMap<String, Object>();
        UserSession userSession = getUserSession(request);

//        {List=[{"id":"1","setupdescribe":"22222222","setupvalue":"111111111"},{"id":"62","setupdescribe":"","setupvalue":"企业付款"}]}
//        System.out.println(generalsetList.get("List").toString());
        Boolean result = false;
        if(generalsetList.containsKey("List")){
            JSONArray js = JSONArray.parseArray(generalsetList.get("List").toString());
            for(Object jo :js){
                JSONObject genjo= JSONObject.fromObject(jo);
                GeneralSet generalset =  new GeneralSet();
                generalset.setId(genjo.getInt("id"));
                generalset.setCompany_id(userSession.getCompanyId());
                generalset.setSetupvalue(genjo.getString("setupvalue"));
//            	 generalset.setSetupdescribe(genjo.getString("setupdescribe"));
                result = generalsetService.updateGeneralSet(generalset);
            }

        }else{
            throw new BizzException(BizzException.error_code_invalid_params, "没有可保存项");
        }
        return new HttpResponseBodyWrapper(result);
    }
    
//    /**
//     * 新增通用设置
//     * @author kelan
//     * @date 2017年4月11日
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @ResponseBody
//    @PostMapping("/generalset")
//    public HttpResponseBodyWrapper postGeneralSet(HttpServletRequest request, @RequestBody GeneralSet generalset) throws Exception {
//
//    	UserSession userSession = getUserSession(request);
//        generalset.setCompany_id(userSession.getCompanyId());
//        if (generalset.getSetupname() == null) {
//            throw new BizzException(BizzException.error_code_invalid_params, "请输入项目名称");
//        }
//        if (generalset.getSetupname() == null) {
//            throw new BizzException(BizzException.error_code_invalid_params, "请输入项目参数");
//        }
//        Boolean result = generalsetService.insertGeneralSet(generalset);
//        return new HttpResponseBodyWrapper(result);
//
//    }
//
//    /**
//     * 通过公司id 和项目id 查询实体类
//     * @author kelan
//     * @date 2017年4月11日
//     * @param request
//     * @param id
//     * @return
//     * @throws Exception
//     */
//    @ResponseBody
//    @GetMapping("/generalset/{id}")
//    public HttpResponseBodyWrapper getGeneralSet(HttpServletRequest request, @PathVariable Integer id) throws Exception {
//       // Map<String, Object> model = new HashMap<String, Object>();
//        UserSession userSession = getUserSession(request);
//        GeneralSet result = generalsetService.getGeneralSet(userSession.getCompanyId(), id);
//        return new HttpResponseBodyWrapper(result);
//    }
   
//    /**
//     * 更新设置
//     * @author kelan
//     * @date 2017年4月11日
//     * @param request
//     * @param generalset
//     * @return
//     * @throws Exception
//     */
//    @ResponseBody
//    @PutMapping("/generalset/{id}")
//    public HttpResponseBodyWrapper putGeneralSet(HttpServletRequest request, @RequestBody GeneralSet generalset) throws Exception {
//
//    	UserSession userSession = getUserSession(request);
//        generalset.setCompany_id(userSession.getCompanyId());
//        if (generalset.getSetupname() == null) {
//            throw new BizzException(BizzException.error_code_invalid_params, "请输入项目名称");
//        }
//        if (generalset.getSetupname() == null) {
//            throw new BizzException(BizzException.error_code_invalid_params, "请输入项目参数");
//        }
//        Boolean result = generalsetService.updateGeneralSet(generalset);
//        return new HttpResponseBodyWrapper(result);
//    }
//
//    @ResponseBody
//    @DeleteMapping("/generalset/{id}")
//    public HttpResponseBodyWrapper deleteGeneralSet(HttpServletRequest request, @PathVariable Integer id) throws Exception {
//        //Map<String, Object> model = new HashMap<String, Object>();
//        UserSession userSession = getUserSession(request);
//        Boolean result = generalsetService.deleteGeneralSet(userSession.getCompanyId(), id);
//        return new HttpResponseBodyWrapper(result);
//    }


    
}
